package service

import (
	"context"
	"user-service/basic/config"
	__ "user-service/basic/proto"
	"user-service/handler/model"
)

func (s *Server) DeparmentList(_ context.Context, in *__.DeparmentListReq) (*__.DeparmentListResp, error) {
	var deparment []model.DepartmentTypeJoin
	config.DB.Model(&model.Department{}).
		Select("departments.department_name, departments.hospital_id,departments.introduction,department_types.type_name").
		Joins("left join department_types on departments.type_id = department_types.id").
		Find(&deparment)

	var Deparmentlist []*__.DeparmentList
	for _, m := range deparment {
		list := __.DeparmentList{
			DepartmentName: m.DepartmentName,
			TypeName:       m.TypeName,
			HospitalId:     m.HospitalId,
			Introduction:   m.Introduction,
		}
		Deparmentlist = append(Deparmentlist, &list)
	}
	return &__.DeparmentListResp{
		List: Deparmentlist,
	}, nil
}
func (s *Server) DeparmentUpdate(_ context.Context, in *__.DeparmentUpdateReq) (*__.DeparmentUpdateResp, error) {
	var deparments model.Department
	err := config.DB.Model(&deparments).Where("id=?", in.Id).Update("status", in.Status).Error
	if err != nil {
		return nil, err
	}
	return &__.DeparmentUpdateResp{
		Id: in.Id,
	}, nil
}
